using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Auxiliaries;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Form for editing enumeration items of a AEUC enumeration setting control
    /// </summary>
    public partial class AEUCEnumerationEditForm : Form
    {
        IAEUCEnumeration enumEdit;
        /// <summary>
        /// Public constructor taking a reference to a Enumeration AEUC control
        /// </summary>
        /// <param name="ienumEdit">Reference to the AEUC control</param>
        public AEUCEnumerationEditForm(IAEUCEnumeration ienumEdit)
        {
            enumEdit = ienumEdit;

            InitializeComponent();

            UpdateCurrentEnumValues();
        }

        private void UpdateCurrentEnumValues()
        {
            cbCurrentEnumItems.Items.Clear();
            GenAEUCEnumerationItem gaeucei = null;
            int i_enum = 0;

            do
            {
                gaeucei = enumEdit.AEUC_GetEnumVal(i_enum++);
                if (gaeucei != null)
                {
                    cbCurrentEnumItems.Items.Add(gaeucei);
                }
            } while (gaeucei != null);

            if (cbCurrentEnumItems.Items.Count > 0)
                cbCurrentEnumItems.Enabled = true;
            else
                cbCurrentEnumItems.Enabled = false;
        }

        private void cbCurrentEnumItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenAEUCEnumerationItem gaeucei = (GenAEUCEnumerationItem)cbCurrentEnumItems.Items[cbCurrentEnumItems.SelectedIndex];
            tbVal.Text = tbCurVal.Text = gaeucei.AEUC_val.ToString();
            tbOptVal.Text = tbCurOptVal.Text = gaeucei.AEUC_optval.ToString();
            tbDisplayName.Text = gaeucei.AEUC_displayname;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Int64 val;
            Int64 opt_val;
            UInt64 tmp_val;
            if (!Int64.TryParse(tbVal.Text, out val))
            {
                if (HEX_converter.ToBinary(tbVal.Text, out tmp_val))
                    val = (Int64)tmp_val;
                else
                    MessageBox.Show("Please input a valid integer for value.");
            }
            if (!Int64.TryParse(tbOptVal.Text, out opt_val))
            {
                if (HEX_converter.ToBinary(tbOptVal.Text, out tmp_val))
                    opt_val = (Int64)tmp_val;
                else
                    MessageBox.Show("Please input a valid integer for optional value.");
            }
            enumEdit.AEUC_AddEnumVal(tbDisplayName.Text, val, opt_val);
            UpdateCurrentEnumValues();
            cbCurrentEnumItems.SelectedIndex = cbCurrentEnumItems.Items.Count - 1; //Select the last added item
        }

        private void btnRemoveItem_Click(object sender, EventArgs e)
        {
            int Position = cbCurrentEnumItems.SelectedIndex;
            enumEdit.AEUC_DeleteEnumVal(Position);
            UpdateCurrentEnumValues();
            if (cbCurrentEnumItems.Enabled)
            {
                if(--Position < 0)
                    Position = 0;
                cbCurrentEnumItems.SelectedIndex = Position; //Select the item prior the delited one
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int val;
            int opt_val;
            UInt64 tmp_val;
            if (!int.TryParse(tbVal.Text, out val))
            {
                if (HEX_converter.ToBinary(tbVal.Text, out tmp_val))
                    val = (int)tmp_val;
                else
                    MessageBox.Show("Please input a valid integer for value.");
            }
            if (!int.TryParse(tbOptVal.Text, out opt_val))
            {
                if (HEX_converter.ToBinary(tbOptVal.Text, out tmp_val))
                    opt_val = (int)tmp_val;
                else
                    MessageBox.Show("Please input a valid integer for optional value.");
            }
            if (cbCurrentEnumItems.Enabled)
            {
                enumEdit.AEUC_EditEnumVal(cbCurrentEnumItems.SelectedIndex, tbDisplayName.Text, val, opt_val);
                UpdateCurrentEnumValues();
                cbCurrentEnumItems.SelectedIndex = cbCurrentEnumItems.SelectedIndex; //Reselect the changed item
            }
            else
            {
                enumEdit.AEUC_AddEnumVal(tbDisplayName.Text, val, opt_val);
                UpdateCurrentEnumValues();
                cbCurrentEnumItems.SelectedIndex = cbCurrentEnumItems.Items.Count - 1; //Select the last added item
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}